﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyDLL
{
    /// <summary>
    /// ID生成器
    /// 参考：https://blog.csdn.net/sz_bdqn/article/details/5878476
    /// </summary>
    public static class IDGenerator
    {
        /// <summary>
        /// 生成string型ID
        /// </summary>
        /// <returns></returns>
        public static string GenerateID_String()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }

            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        /// <summary>
        /// 生成long型ID
        /// </summary>
        /// <returns></returns>
        public static long GenerateID_Long()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// 根据时间生成string型ID
        /// </summary>
        /// <returns></returns>
        public static string GenerateID_DateTime()
        {
            return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), Guid.NewGuid().ToString().Split('-')[4]);
        }
    }
}
